package com.dongke.controller;

import com.dongke.pojo.Menu1;
import com.dongke.pojo.User1;
import com.dongke.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menuController")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("getMenu")
    @ResponseBody
    public List<Map<String, Object>> getMenu(HttpServletRequest request){
        HttpSession session = request.getSession();
        User1 user1 = (User1) session.getAttribute("list");
        List<Map<String, Object>> list = menuService.getMenu(user1.getUid());
        return list;
    }

    @RequestMapping("show")
    public String toshow(){

        return "menu/show";
    }
    //菜单列表页面数据查询展示
    @RequestMapping("getMenuZtree")
    @ResponseBody
    public List<Map<String, Object>> getMenuZtree(){
        List<Map<String, Object>> list = menuService.getMenuZtree();
        return list;
    }

    @RequestMapping("toAdd")
    public String toAdd(){

        return "menu/add";
    }
    @RequestMapping("addMenuInfo")
    @ResponseBody
    public boolean addMenuInfo(Menu1 menu1){

        return menuService.addInfo(menu1);
    }

    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id, Model model){
        Menu1 menu = menuService.getInfo(id);
        model.addAttribute("menu", menu);
        return "menu/update";
    }
    //将修改后的数据填充至数据库
    @RequestMapping("updateMenuInfo")
    @ResponseBody
    public boolean updateMenuInfo(Menu1 menu){
        return menuService.updateMenuInfo(menu);
    }
    //删除后台目录页面
    @RequestMapping("deleteMenuInfo/{id}")
    @ResponseBody
    public boolean deleteMenuInfo(@PathVariable("id")Integer id){
        return menuService.deleteMenuInfo(id);
    }
    //查询角色所拥有的权限返回给修改页面
    @RequestMapping("getZtreeCheked/{rid}")
    @ResponseBody
    public List<Map<String, Object>> getZtreeCheked(@PathVariable("rid") Integer rid){
        List<Map<String, Object>> list = menuService.getZtreeCheked(rid);
        return list;
    }
}
